package com.yb.web.controller.model;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yb.framework.core.domain.AjaxResult;
import com.yb.framework.neo4j.service.Neo4jService;
import com.yb.web.controller.base.BaseController;

@Controller
@Scope("prototype")
@RequestMapping("/model/neo4j")
public class ModelNeo4jController extends BaseController {
	
	@Autowired
	private Neo4jService neo4jService;
	
	Logger logger = LoggerFactory.getLogger(ModelNeo4jController.class.getName());
	
	private static String MODEL_NEO4J_CHART = "model/neo4j/chart";
	private static String MODEL_NEO4J_SHAPE = "model/neo4j/shape_chart";
	private static String MODEL_NEO4J_TEMPLATE = "model/neo4j/template_chart";
	private static String MODEL_NEO4J_THING = "model/neo4j/thing_chart";
	
	@GetMapping("/shape")
	@PreAuthorize("hasAuthority('model:neo4j:shape')")
	public String shape() {
		return MODEL_NEO4J_SHAPE;
	}
	
	@GetMapping("/template")
	@PreAuthorize("hasAuthority('model:neo4j:template')")
	public String template() {
		return MODEL_NEO4J_TEMPLATE;
	}
	
	@GetMapping("/thing")
	@PreAuthorize("hasAuthority('model:neo4j:thing')")
	public String thing() {
		return MODEL_NEO4J_THING;
	}
	
	@GetMapping("/template/chart")
	@PreAuthorize("hasAuthority('model:neo4j:template:chart')")
	public String templateChart(@RequestParam("label") String label, @RequestParam("nodeUuid") String nodeUuid, ModelMap model) {
		model.put("label", label);
		model.put("nodeUuid", nodeUuid);
		return MODEL_NEO4J_CHART;
	}
	
	@GetMapping("/shape/chart")
	@PreAuthorize("hasAuthority('model:neo4j:shape:chart')")
	public String shapeChart(@RequestParam("label") String label, @RequestParam("nodeUuid") String nodeUuid, ModelMap model) {
		model.put("label", label);
		model.put("nodeUuid", nodeUuid);
		return MODEL_NEO4J_CHART;
	}
	
	@GetMapping("/thing/chart")
	@PreAuthorize("hasAuthority('model:neo4j:thing:chart')")
	public String thingChart(@RequestParam("label") String label, @RequestParam("nodeUuid") String nodeUuid, ModelMap model) {
		model.put("label", label);
		model.put("nodeUuid", nodeUuid);
		return MODEL_NEO4J_CHART;
	}
	
	@ResponseBody
	@RequestMapping("/chart/getInfo")
	public AjaxResult getChartInfo(@RequestParam("label") String label, @RequestParam("nodeUuid") String nodeUuid) {
		JSONObject allNodes = neo4jService.getAllNodes(label, nodeUuid);
		JSONObject chartObj = new JSONObject();
		JSONArray chartDataList = new JSONArray();
		JSONArray chartLinkList = new JSONArray();
		JSONArray nodeList = allNodes.getJSONArray("nodeList");
		JSONArray shipList = allNodes.getJSONArray("shipList");
		if(nodeList != null && nodeList.size() > 0) {
			for (int i = 0; i < nodeList.size(); i++) {
				JSONObject node = nodeList.getJSONObject(i);
				JSONObject chartData = new JSONObject();
				chartData.put("id", node.getString("nodeId"));
				chartData.put("uuid", node.getString("uuid"));
				chartData.put("name", node.getString("name"));
				chartData.put("category", node.getString("label"));
				chartData.put("symbolSize", node.getInteger("symbolSize"));
				chartDataList.add(chartData);
			}
		}
		if(shipList != null && shipList.size() > 0) {
			for (int j = 0; j < shipList.size(); j++) {
				JSONObject ship = shipList.getJSONObject(j);
				JSONObject chartLink = new JSONObject();
				chartLink.put("name", "");
				chartLink.put("source", ship.getString("startNodeId"));
				chartLink.put("target", ship.getString("endNodeId"));
				chartLinkList.add(chartLink);
			}
		}
		chartObj.put("data", chartDataList);
		chartObj.put("links", chartLinkList);
		
		return AjaxResult.success(chartObj);
	}
	
	@ResponseBody
	@RequestMapping("/getParamNames")
	public AjaxResult getParamNames(@RequestParam("label") String label, @RequestParam("nodeUuid") String nodeUuid) {
		JSONArray paramNames = neo4jService.getParamNames(label, nodeUuid);
		return AjaxResult.success(paramNames);
	}
	
	@ResponseBody
	@RequestMapping("/getAllParamNames")
	public AjaxResult getAllParamNames(@RequestParam("label") String label, @RequestParam("nodeUuid") String nodeUuid) {
		JSONArray allParamNames = neo4jService.getAllParamNames(label, nodeUuid);
		return AjaxResult.success(allParamNames);
	}

	@ResponseBody
	@RequestMapping("/deleteLabel/{label}")
	public AjaxResult deleteLabel(@PathVariable String lable) {
		neo4jService.deleteLabel(lable);
		return AjaxResult.success("删除成功");
	}
	
	@ResponseBody
	@RequestMapping("/deleteAll")
	public AjaxResult deleteAll() {
		neo4jService.deleteLabel("thing");
		neo4jService.deleteLabel("template");
		neo4jService.deleteLabel("shape");
		neo4jService.deleteLabel("param");
		return AjaxResult.success("删除成功");
	}
}